﻿using Digitalmes.Model;
using Digitalmes.Model.FactoryManagement;
using Digitalmes.Model.ProcessManagement;
using Digitalmes.ViewModel.ProcessManagement.ProcessRouteVMs;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using WalkingTec.Mvvm.Mvc;

namespace Digitalmes.ProcessManagement.Controllers
{
    [AuthorizeJwtWithCookie]
    [ActionDescription("_Model.ProcessRoute")]
    [ApiController]
    [Route("/api/ProcessManagement/ProcessRoute")]
    public partial class ProcessRouteController : BaseApiController
    {
        [ActionDescription("Sys.Get")]
        [HttpGet("{id}")]
        public IActionResult Get(string id)
        {
            var vm = Wtm.CreateVM<ProcessRouteVM>(id);
            return Ok(vm);
        }

        [ActionDescription("Sys.Create")]
        [HttpPost("[action]")]
        public async Task<IActionResult> Create(ProcessRouteVM vm)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState.GetErrorJson());
            }
            else
            {
                await vm.DoAddAsync();

                if (!ModelState.IsValid)
                {
                    return BadRequest(ModelState.GetErrorJson());
                }
                else
                {
                    return Ok(vm.Entity);
                }
            }

        }

        [ActionDescription("Sys.Edit")]
        [HttpPut("[action]")]
        public async Task<IActionResult> Edit(ProcessRouteVM vm)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState.GetErrorJson());
            }
            else
            {
                await vm.DoEditAsync(false);
                if (!ModelState.IsValid)
                {
                    return BadRequest(ModelState.GetErrorJson());
                }
                else
                {
                    return Ok(vm.Entity);
                }
            }
        }


        [HttpPost("BatchEdit")]
        [ActionDescription("Sys.BatchEdit")]
        public ActionResult BatchEdit(ProcessRouteBatchVM vm)
        {
            if (!ModelState.IsValid || !vm.DoBatchEdit())
            {
                return BadRequest(ModelState.GetErrorJson());
            }
            else
            {
                return Ok(vm.Ids.Count());
            }
        }

        [HttpPost("BatchDelete")]
        [ActionDescription("Sys.Delete")]
        public IActionResult BatchDelete([FromBody] string[] ids)
        {
            var vm = Wtm.CreateVM<ProcessRouteBatchVM>();
            if (ids != null && ids.Count() > 0)
            {
                vm.Ids = ids;
            }
            else
            {
                return Ok();
            }
            if (!ModelState.IsValid || !vm.DoBatchDelete())
            {
                return BadRequest(ModelState.GetErrorJson());
            }
            else
            {
                return Ok(ids.Count());
            }
        }

        [ActionDescription("Sys.DownloadTemplate")]
        [HttpGet("GetExcelTemplate")]
        public IActionResult GetExcelTemplate()
        {
            var vm = Wtm.CreateVM<ProcessRouteImportVM>();
            var qs = new Dictionary<string, string>();
            foreach (var item in Request.Query.Keys)
            {
                qs.Add(item, Request.Query[item]);
            }
            vm.SetParms(qs);
            var data = vm.GenerateTemplate(out string fileName);
            return File(data, "application/vnd.ms-excel", fileName);
        }

        [ActionDescription("Sys.Import")]
        [HttpPost("Import")]
        public ActionResult Import(ProcessRouteImportVM vm)
        {

            if (vm.ErrorListVM.EntityList.Count > 0 || !vm.BatchSaveData())
            {
                return BadRequest(vm.GetErrorJson());
            }
            else
            {
                return Ok(vm.EntityList.Count);
            }
        }


        [Public]
        [HttpGet("[action]")]
        public ActionResult GetProductionLine()
        {
            var rv = DC.Set<ProductionLine>().GetSelectListItems(Wtm, x => x.Name);

            return Ok(rv);

        }



        [Public]
        [HttpGet("[action]")]
        public ActionResult GetProductionLineByProduct(string id)
        {
            var rv = DC.Set<ProductProductionLine>().Include(x => x.ProductionLine)
                .CheckID(id, x => x.ProductId)
                .GetSelectListItems(Wtm, y => y.ProductionLine.Code, k => k.ProductionLine.ID);
            return Ok(rv);
        }

        [Public]
        [HttpGet("[action]")]
        public ActionResult GetProductByProductionLine(string id)
        {
            var rv = DC.Set<ProductProductionLine>()
                .Include(x => x.Product)
                .CheckID(id, x => x.ProductionLineId)
                .GetSelectListItems(Wtm, y => y.Product.Code, k => k.Product.ID);
            return Ok(rv);
        }

        [Public]
        [HttpGet("[action]")]
        public ActionResult GetEquipmentByProductionLine(string id)
        {
            if (string.IsNullOrEmpty(id)  || id == "null")
            {
                return Ok(new ComboSelectListItem());
            }

            var rv = DC.Set<EquipmentProductionLine>().Include(x => x.Equipment)
                .CheckID(id, x => x.ProductionLineId)
                .GetSelectListItems(Wtm, y => y.Equipment.EquipName, k => k.Equipment.ID);
            return Ok(rv);
        }

    }
}